home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / make / macro.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  4KB  |  201 lines

  1. /*************************************************************************
  2.  *
  3.  *  m a k e :   m a c r o . c
  4.  *
  5.  *  Macro control for make
  6.  *========================================================================
  7.  * Edition history
  8.  *
  9.  *  #    Date                         Comments                       By
  10.  * --- -------- ---------------------------------------------------- ---
  11.  *   1    ??                                                         ??
  12.  *   2 23.08.89 Error message corrected                              RAL
  13.  *   3 30.08.89 macro flags added, indention ch.                     PSH,RAL
  14.  *   4 03.09.89 fixed LZ eliminated, doexp(...) changed              RAL
  15.  *   5 06.09.89 M_MAKE added, setDFmacro added                       RAL
  16.  *   6 20.09.89 work around for Minix PC ACK bug                     BE,RAL
  17.  * ------------ Version 2.0 released ------------------------------- RAL
  18.  *
  19.  *************************************************************************/
  20.  
  21. #include "h.h"
  22.  
  23.  
  24. static char   buf[256];
  25.  
  26. struct macro *getmp(name)
  27. char *name;
  28. {
  29.   register struct macro *rp;
  30.  
  31.   for (rp = macrohead; rp; rp = rp->m_next)
  32.         if (strcmp(name, rp->m_name) == 0)
  33.             return rp;
  34.   return (struct macro *)0;
  35. }
  36.  
  37.  
  38. char *getmacro(name)
  39. char *name;
  40. {
  41.   struct macro *mp;
  42.  
  43.   if (mp = getmp(name))
  44.         return mp->m_val;
  45. /*    else*/
  46.         return "";
  47. }
  48.  
  49.  
  50. struct macro *setmacro(name, val)
  51. char *name;
  52. char *val;
  53. {
  54.   register struct macro *rp;
  55.   register char         *cp;
  56.  
  57.  
  58.         /*  Replace macro definition if it exists  */
  59.   for (rp = macrohead; rp; rp = rp->m_next)
  60.     if (strcmp(name, rp->m_name) == 0) {
  61.         if(rp->m_flag & M_OVERRIDE) return rp;    /* mustn't change */
  62.         free(rp->m_val);    /*  Free space from old  */
  63.         break;
  64.         }
  65.  
  66.     if (!rp)        /*  If not defined, allocate space for new  */
  67.     {
  68.         if ((rp = (struct macro *)malloc(sizeof (struct macro)))
  69.                      == (struct macro *)0)
  70.             fatal("No memory for macro",(char *)0,0);
  71.  
  72.         rp->m_next = macrohead;
  73.         macrohead = rp;
  74.         rp->m_flag = FALSE;
  75.  
  76.         if ((cp = malloc(strlen(name)+1)) == (char *)0)
  77.             fatal("No memory for macro",(char *)0,0);
  78.         strcpy(cp, name);
  79.         rp->m_name = cp;
  80.     }
  81.  
  82.     if ((cp = malloc(strlen(val)+1)) == (char *)0)
  83.         fatal("No memory for macro",(char *)0,0);
  84.     strcpy(cp, val);        /*  Copy in new value  */
  85.     rp->m_val = cp;
  86.  
  87.   return rp;
  88. }
  89.  
  90.  
  91. void setDFmacro(name, val)
  92. char *name;
  93. char *val;
  94. {
  95.   char        *c,*tmp;
  96.   int          len;
  97.   static char  filename[]="@F";
  98.   static char  dirname[] ="@D";
  99.  
  100.   setmacro(name,val);
  101.   *filename = *name;
  102.   *dirname  = *name;
  103.   /* Null string -- not defined macro */
  104.   if ( !(*val)) {
  105.      setmacro(filename,"");
  106.      setmacro(dirname,"");
  107.      return;
  108.   }
  109.   if (!(c = strrchr(val,(int)'/'))) {
  110.      setmacro(filename,val);
  111.      setmacro(dirname,"./");
  112.      return;
  113.   }
  114.   setmacro(filename,c+1);
  115.   len = c - val + 1;
  116.   if((tmp = malloc(len + 1)) == (char *) 0)
  117.      fatal("No memory for tmp",(char *)0,0);
  118.   strncpy(tmp,val,len);
  119.   tmp[len] = '\0';
  120.   setmacro(dirname,tmp);
  121.   free(tmp);
  122.   return;
  123. }
  124.  
  125. /*
  126.  *    Do the dirty work for expand
  127.  */
  128. void doexp(to, from)
  129. struct str *to;
  130. char  *from;
  131. {
  132.   register char *rp;
  133.   register char *p;
  134.   char *q;
  135.   struct macro *mp;
  136.   int temp;            /* work-around for ACK bug (PC)*/
  137.  
  138.  
  139.   rp  = from;
  140.   temp= to->pos;
  141.   p   = &(*to->ptr)[temp];
  142.   while (*rp) {
  143.     if (*rp != '$') {
  144.         *p++ = *rp++;
  145.         to->pos++;
  146.     }
  147.     else {
  148.         q = buf;
  149.         if (*++rp == '{')
  150.             while (*++rp && *rp != '}')
  151.                 *q++ = *rp;
  152.         else if (*rp == '(')
  153.             while (*++rp && *rp != ')')
  154.                 *q++ = *rp;
  155.         else if (!*rp) {
  156.             *p++ = '$';
  157.             to->pos++;
  158.             break;
  159.         }
  160.         else
  161.             *q++ = *rp;
  162.         *q = '\0';
  163.         if (*rp)
  164.             rp++;
  165.         if (!(mp = getmp(buf)))
  166.             mp = setmacro(buf, "");
  167.         if (mp->m_flag & M_MARK)
  168.             fatal("Infinitely recursive macro %s", mp->m_name,0);
  169.         mp->m_flag |= M_MARK;
  170.         if ( mp->m_flag & M_MAKE) expmake = TRUE;
  171.         doexp(to, mp->m_val);
  172.         temp = to->pos;
  173.         p = &(*to->ptr)[temp];
  174.         mp->m_flag &= ~M_MARK;
  175.     }
  176.     if (to->pos >= to->len) {
  177.         strrealloc (to);
  178.         temp = to->pos;
  179.         p =  &(*to->ptr)[temp];
  180.     }
  181.   }
  182.   *p = '\0';
  183. }
  184.  
  185.  
  186. /*
  187.  *    Expand any macros in str.
  188.  */
  189. void expand(strs)
  190. struct str *strs;
  191. {
  192.   char  *a;
  193.  
  194.   if ((a = malloc(strlen(*strs->ptr)+1)) == (char *)0)
  195.      fatal("No memory for temporary string",(char *)0,0);
  196.   strcpy(a, *strs->ptr);
  197.   strs->pos = 0;
  198.   doexp(strs, a);
  199.   free(a);
  200. }
  201.